//
//  InviteView.swift
//  CMB250716
//
//  Created by Trae AI on 2025/01/15.
//

import SwiftUI
import Foundation

struct InviteView: View {
    @EnvironmentObject var dataManager: DataManager
    @Environment(\.presentationMode) var presentationMode
    
    @State private var inviteCode = ""
    @State private var showingShareSheet = false
    @State private var showingAlert = false
    @State private var alertMessage = ""
    @State private var animationOffset: CGFloat = 0
    @State private var glowAnimation: Bool = false
    @State private var generatedCode = ""
    @State private var showGeneratedCode = false
    @State private var showingLoginAlert = false
    @State private var showingLoginView = false
    
    private var language: String {
        return dataManager.currentLanguage
    }
    
    private var shareText: String {
        let code = showGeneratedCode ? generatedCode : generateInviteCode()
        return language == "zh" ? 
            AppStrings.Invite.shareText(inviteCode: code) :
            AppStrings.Invite.shareTextEn(inviteCode: code)
    }
    
    var body: some View {
        ZStack {
            // 高级4D背景效果
            premiumBackgroundView
            
            VStack(spacing: 0) {
                // 顶部导航栏
                topNavigationBar
                
                ScrollView {
                    VStack(spacing: 10) {
                        // Shareget3 视频
                        shareget3VideoSection
                        
                        // Plus2 视频
                        plus2VideoSection
                        
                        // 邀请码和分享按钮区域
                        inviteCodeAndShareSection
                        
                        // 邀请说明文本
                        inviteDescriptionSection
                    }
                    .padding(.horizontal, 20)
                    .padding(.bottom, 40)
                }
            }
        }
        .alert(isPresented: $showingAlert) {
            Alert(
                title: Text(language == "zh" ? "提示" : "Notice"),
                message: Text(alertMessage),
                dismissButton: .default(Text(language == "zh" ? "确定" : "OK"))
            )
        }
        .sheet(isPresented: $showingShareSheet) {
            ActivityViewController(activityItems: [shareText])
        }
        .alert(
            language == "zh" ? "需要登录" : "Login Required",
            isPresented: $showingLoginAlert
        ) {
            Button(
                language == "zh" ? "去登录" : "OK",
                role: .none
            ) {
                showingLoginView = true
            }
            
            Button(
                language == "zh" ? "取消" : "Cancel",
                role: .cancel
            ) { }
        } message: {
            Text(
                language == "zh" ? 
                "生成邀请码需要先登录账户。" : 
                "Please login to generate invitation code."
            )
        }
        .sheet(isPresented: $showingLoginView) {
            LoginView()
                .environmentObject(dataManager)
        }
        .navigationBarHidden(true)
        .onAppear {
            startAnimations()
        }
    }
    
    // MARK: - 顶部导航栏
    private var topNavigationBar: some View {
        HStack {
            Button(action: {
                presentationMode.wrappedValue.dismiss()
            }) {
                Image(systemName: "chevron.left")
                    .font(.system(size: 18, weight: .medium))
                    .foregroundColor(Color(red: 1.0, green: 0.6, blue: 0.2))
            }
            
            Spacer()
            
            Text(language == "zh" ? "邀请好友" : "Invite Friends")
                .font(.system(size: 20, weight: .bold))
                .foregroundColor(.white)
            
            Spacer()
            
            Button(language == "zh" ? "完成" : "Done") {
                presentationMode.wrappedValue.dismiss()
            }
            .foregroundColor(Color(red: 1.0, green: 0.6, blue: 0.2))
        }
        .padding(.horizontal, 20)
        .padding(.top, 8)
        .padding(.bottom, 16)
    }
    
    // MARK: - Shareget3 视频区域
    private var shareget3VideoSection: some View {
        MP4VideoView(videoName: "Shareget3")
            .frame(height: 120)
            .frame(maxWidth: .infinity)
            .cornerRadius(15)
            .overlay(
                RoundedRectangle(cornerRadius: 15)
                    .stroke(
                        LinearGradient(
                            gradient: Gradient(colors: [
                                Color(red: 1.0, green: 0.84, blue: 0.0), // 金色
                                Color(red: 0.8, green: 0.6, blue: 0.0),  // 深金色
                                Color.black.opacity(0.8)                  // 黑色
                            ]),
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 0.7
                    )
            )
            .shadow(
                color: Color(red: 1.0, green: 0.84, blue: 0.0).opacity(0.6),
                radius: 8,
                x: 0,
                y: 4
            )
            .shadow(
                color: Color.black.opacity(0.3),
                radius: 12,
                x: 0,
                y: 8
            )
            .padding(.vertical, 10)
    }
    
    // MARK: - Plus2 视频区域
    private var plus2VideoSection: some View {
        MP4VideoView(videoName: "Plus2")
            .frame(height: 120)
            .frame(maxWidth: .infinity)
            .cornerRadius(15)
            .overlay(
                RoundedRectangle(cornerRadius: 15)
                    .stroke(
                        LinearGradient(
                            gradient: Gradient(colors: [
                                Color(red: 1.0, green: 0.84, blue: 0.0), // 金色
                                Color(red: 0.8, green: 0.6, blue: 0.0),  // 深金色
                                Color.black.opacity(0.8)                  // 黑色
                            ]),
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 0.7
                    )
            )
            .shadow(
                color: Color(red: 1.0, green: 0.84, blue: 0.0).opacity(0.6),
                radius: 8,
                x: 0,
                y: 4
            )
            .shadow(
                color: Color.black.opacity(0.3),
                radius: 12,
                x: 0,
                y: 8
            )
            .padding(.vertical, 10)
    }
    
    // MARK: - 邀请码和分享按钮区域
    private var inviteCodeAndShareSection: some View {
        VStack(spacing: 20) {
            // 生成邀请码按钮
            Button(action: {
                generateAndShowInviteCode()
            }) {
                Text(language == "zh" ? AppStrings.Invite.yourInviteCode : AppStrings.Invite.yourInviteCodeEn)
                    .font(.system(size: 18, weight: .bold, design: .rounded))
                    .foregroundColor(.white)
                    .padding(.vertical, 14)
                    .padding(.horizontal, 28)
                    .background(
                        ZStack {
                            // 主背景
                            RoundedRectangle(cornerRadius: 16)
                                .fill(
                                    LinearGradient(
                                        colors: [
                                            Color(red: 0.2, green: 0.5, blue: 1.0),
                                            Color(red: 0.1, green: 0.4, blue: 0.9),
                                            Color(red: 0.05, green: 0.3, blue: 0.8)
                                        ],
                                        startPoint: .topLeading,
                                        endPoint: .bottomTrailing
                                    )
                                )
                            
                            // 3D高光效果
                            RoundedRectangle(cornerRadius: 16)
                                .fill(
                                    LinearGradient(
                                        colors: [
                                            Color.white.opacity(0.3),
                                            Color.clear
                                        ],
                                        startPoint: .top,
                                        endPoint: .center
                                    )
                                )
                        }
                    )
                    .overlay(
                        RoundedRectangle(cornerRadius: 16)
                            .stroke(
                                LinearGradient(
                                    colors: [
                                        Color.white.opacity(0.4),
                                        Color.white.opacity(0.1)
                                    ],
                                    startPoint: .topLeading,
                                    endPoint: .bottomTrailing
                                ),
                                lineWidth: 1
                            )
                    )
                    .shadow(color: Color(red: 0.2, green: 0.5, blue: 1.0).opacity(0.4), radius: 8, x: 0, y: 4)
                    .shadow(color: .black.opacity(0.2), radius: 12, x: 0, y: 6)
            }
            .buttonStyle(PremiumButtonStyle())
            
            // 生成的邀请码显示区域
            if showGeneratedCode {
                HStack(spacing: 16) {
                    // 邀请码显示区域
                    Text(generatedCode)
                        .font(.custom("ZING RUST BASE ROUGH", size: 44))
                        .fontWeight(.bold)
                        .foregroundStyle(
                            LinearGradient(
                                colors: [
                                    Color(red: 1.0, green: 0.84, blue: 0.0),
                                    Color(red: 1.0, green: 0.7, blue: 0.3),
                                    Color(red: 0.9, green: 0.6, blue: 0.1)
                                ],
                                startPoint: .topLeading,
                                endPoint: .bottomTrailing
                            )
                        )
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 12)
                
                    // 分享按钮
                    Button(action: {
                        showingShareSheet = true
                    }) {
                        HStack(spacing: 8) {
                            Image(systemName: "square.and.arrow.up")
                                .font(.system(size: 18, weight: .semibold))
                            Text(language == "zh" ? "分享" : "Share")
                                .font(.system(size: 18, weight: .semibold))
                        }
                        .foregroundColor(.white)
                        .padding(.horizontal, 24)
                        .padding(.vertical, 22)
                        .background(
                            ZStack {
                                RoundedRectangle(cornerRadius: 16)
                                    .fill(
                                        LinearGradient(
                                            colors: [
                                                Color(red: 0.2, green: 0.5, blue: 1.0),
                                                Color(red: 0.1, green: 0.4, blue: 0.9),
                                                Color(red: 0.05, green: 0.3, blue: 0.8)
                                            ],
                                            startPoint: .topLeading,
                                            endPoint: .bottomTrailing
                                        )
                                    )
                                
                                RoundedRectangle(cornerRadius: 16)
                                    .fill(
                                        LinearGradient(
                                            colors: [
                                                Color.white.opacity(0.3),
                                                Color.clear
                                            ],
                                            startPoint: .top,
                                            endPoint: .center
                                        )
                                    )
                            }
                        )
                        .overlay(
                            RoundedRectangle(cornerRadius: 16)
                                .stroke(
                                    LinearGradient(
                                        colors: [
                                            Color.white.opacity(0.4),
                                            Color.white.opacity(0.1)
                                        ],
                                        startPoint: .topLeading,
                                        endPoint: .bottomTrailing
                                    ),
                                    lineWidth: 1
                                )
                        )
                        .shadow(color: Color(red: 0.2, green: 0.5, blue: 1.0).opacity(0.4), radius: 8, x: 0, y: 4)
                        .shadow(color: .black.opacity(0.2), radius: 12, x: 0, y: 6)
                    }
                }
                .padding(.horizontal, 20)
                .padding(.vertical, 18)
                .background(
                    ZStack {
                        // 主背景
                        RoundedRectangle(cornerRadius: 18)
                            .fill(
                                LinearGradient(
                                    colors: [
                                        Color(red: 0.15, green: 0.15, blue: 0.18),
                                        Color(red: 0.12, green: 0.12, blue: 0.15),
                                        Color(red: 0.09, green: 0.09, blue: 0.12)
                                    ],
                                    startPoint: .topLeading,
                                    endPoint: .bottomTrailing
                                )
                            )
                        
                        // 3D高光效果
                        RoundedRectangle(cornerRadius: 18)
                            .fill(
                                LinearGradient(
                                    colors: [
                                        Color.white.opacity(0.1),
                                        Color.clear
                                    ],
                                    startPoint: .top,
                                    endPoint: .center
                                )
                            )
                        
                        // 内部光晕
                        RoundedRectangle(cornerRadius: 18)
                            .fill(
                                RadialGradient(
                                    colors: [
                                        Color(red: 1.0, green: 0.84, blue: 0.0).opacity(0.08),
                                        Color.clear
                                    ],
                                    center: .center,
                                    startRadius: 10,
                                    endRadius: 80
                                )
                            )
                    }
                )
                .overlay(
                    RoundedRectangle(cornerRadius: 18)
                        .stroke(
                            LinearGradient(
                                colors: [
                                    Color.white.opacity(0.2),
                                    Color.white.opacity(0.05)
                                ],
                                startPoint: .topLeading,
                                endPoint: .bottomTrailing
                            ),
                            lineWidth: 1
                        )
                )
                .shadow(color: .black.opacity(0.3), radius: 12, x: 0, y: 6)
                .shadow(color: .black.opacity(0.1), radius: 20, x: 0, y: 10)
            }
        }
        .padding(24)
        .background(
            ZStack {
                // 主背景
                RoundedRectangle(cornerRadius: 24)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color(red: 0.06, green: 0.06, blue: 0.12),
                                Color(red: 0.12, green: 0.12, blue: 0.18),
                                Color(red: 0.09, green: 0.09, blue: 0.15)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                
                // 3D高光效果
                RoundedRectangle(cornerRadius: 24)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.08),
                                Color.clear
                            ],
                            startPoint: .top,
                            endPoint: .center
                        )
                    )
                
                // 内部光晕效果
                RoundedRectangle(cornerRadius: 24)
                    .fill(
                        RadialGradient(
                            colors: [
                                Color(red: 1.0, green: 0.84, blue: 0.0).opacity(0.06),
                                Color.clear
                            ],
                            center: .topLeading,
                            startRadius: 30,
                            endRadius: 150
                        )
                    )
            }
        )
        .overlay(
            RoundedRectangle(cornerRadius: 24)
                .stroke(
                    LinearGradient(
                        colors: [
                            Color.white.opacity(0.15),
                            Color.white.opacity(0.03)
                        ],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    ),
                    lineWidth: 1
                )
        )
        .shadow(color: .black.opacity(0.4), radius: 20, x: 0, y: 10)
        .shadow(color: .black.opacity(0.2), radius: 30, x: 0, y: 15)
    }
    
    // MARK: - 邀请奖励区域
    private var rewardsSection: some View {
        VStack(spacing: 20) {
            Text(language == "zh" ? AppStrings.Invite.inviteRewards : AppStrings.Invite.inviteRewardsEn)
                .font(.system(size: 22, weight: .bold, design: .rounded))
                .foregroundStyle(
                    LinearGradient(
                        colors: [.white, Color.white.opacity(0.9)],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )
                .shadow(color: .black.opacity(0.3), radius: 2, x: 0, y: 1)
            
            VStack(spacing: 16) {
                // 立即奖励
                premiumRewardItem(
                    icon: "gift.fill",
                    iconColor: Color(red: 0.2, green: 0.8, blue: 0.4),
                    title: language == "zh" ? AppStrings.Invite.immediateReward : AppStrings.Invite.immediateRewardEn,
                    description: language == "zh" ? AppStrings.Invite.immediateRewardDesc : AppStrings.Invite.immediateRewardDescEn,
                    accentColor: Color(red: 0.2, green: 0.8, blue: 0.4)
                )
                
                // 好友奖励
                premiumRewardItem(
                    icon: "person.badge.plus",
                    iconColor: Color(red: 0.3, green: 0.6, blue: 1.0),
                    title: language == "zh" ? AppStrings.Invite.friendBonus : AppStrings.Invite.friendBonusEn,
                    description: language == "zh" ? AppStrings.Invite.friendBonusDesc : AppStrings.Invite.friendBonusDescEn,
                    accentColor: Color(red: 0.3, green: 0.6, blue: 1.0)
                )
                
                // 消费奖励
                premiumRewardItem(
                    icon: "star.fill",
                    iconColor: Color(red: 1.0, green: 0.6, blue: 0.2),
                    title: language == "zh" ? AppStrings.Invite.consumptionBonus : AppStrings.Invite.consumptionBonusEn,
                    description: language == "zh" ? AppStrings.Invite.consumptionBonusDesc : AppStrings.Invite.consumptionBonusDescEn,
                    accentColor: Color(red: 1.0, green: 0.6, blue: 0.2)
                )
            }
        }
        .padding(28)
        .background(
            ZStack {
                // 主背景
                RoundedRectangle(cornerRadius: 24)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color(red: 0.9, green: 0.7, blue: 0.4).opacity(0.9),
                                Color(red: 0.8, green: 0.6, blue: 0.3).opacity(0.95),
                                Color(red: 0.7, green: 0.5, blue: 0.2).opacity(0.9)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                
                // 外边框
                RoundedRectangle(cornerRadius: 24)
                    .stroke(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.2),
                                Color.white.opacity(0.05),
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 1
                    )
                
                // 内部光效
                RoundedRectangle(cornerRadius: 24)
                    .fill(
                        RadialGradient(
                            colors: [
                                Color(red: 1.0, green: 0.8, blue: 0.5).opacity(0.15),
                                Color.clear
                            ],
                            center: .topLeading,
                            startRadius: 20,
                            endRadius: 120
                        )
                    )
            }
        )
        .shadow(color: .black.opacity(0.3), radius: 12, x: 0, y: 6)
    }
    
    // MARK: - 邀请说明文本
     private var inviteDescriptionSection: some View {
         VStack(spacing: 16) {
             VStack(alignment: .leading, spacing: 12) {
                 // 标题
                 HStack {
                     Image(systemName: "info.circle.fill")
                         .font(.system(size: 16, weight: .semibold))
                         .foregroundColor(Color(red: 1.0, green: 0.8, blue: 0.4))
                     
                     Text(language == "zh" ? "邀请说明" : "Invitation Details")
                         .font(.system(size: 18, weight: .bold, design: .rounded))
                         .foregroundColor(.white)
                     
                     Spacer()
                 }
                 
                 // 分隔线
                 Rectangle()
                     .fill(
                         LinearGradient(
                             colors: [
                                 Color(red: 1.0, green: 0.8, blue: 0.4).opacity(0.6),
                                 Color(red: 1.0, green: 0.6, blue: 0.2).opacity(0.3)
                             ],
                             startPoint: .leading,
                             endPoint: .trailing
                         )
                     )
                     .frame(height: 1)
                 
                 // 描述文本
                 Text(language == "zh" ? AppStrings.Invite.inviteDescription : AppStrings.Invite.inviteDescriptionEn)
                     .font(.system(size: 15, weight: .medium, design: .rounded))
                     .foregroundColor(.white.opacity(0.95))
                     .multilineTextAlignment(.leading)
                     .lineSpacing(4)
                     .lineLimit(nil)
             }
             .padding(20)
             .background(
                 ZStack {
                     // 主背景
                     RoundedRectangle(cornerRadius: 16)
                         .fill(
                             LinearGradient(
                                 colors: [
                                     Color(red: 0.15, green: 0.15, blue: 0.25).opacity(0.85),
                                     Color(red: 0.1, green: 0.1, blue: 0.2).opacity(0.9)
                                 ],
                                 startPoint: .topLeading,
                                 endPoint: .bottomTrailing
                             )
                         )
                    
                     // 边框光效
                     RoundedRectangle(cornerRadius: 16)
                         .stroke(
                             LinearGradient(
                                 colors: [
                                     Color(red: 1.0, green: 0.8, blue: 0.4).opacity(0.5),
                                     Color(red: 1.0, green: 0.6, blue: 0.2).opacity(0.2)
                                 ],
                                 startPoint: .topLeading,
                                 endPoint: .bottomTrailing
                             ),
                             lineWidth: 1.2
                         )
                    
                     // 内部光晕
                     RoundedRectangle(cornerRadius: 16)
                         .fill(
                             RadialGradient(
                                 colors: [
                                     Color(red: 1.0, green: 0.8, blue: 0.5).opacity(0.08),
                                     Color.clear
                                 ],
                                 center: .topLeading,
                                 startRadius: 20,
                                 endRadius: 120
                             )
                         )
                 }
             )
             .shadow(color: .black.opacity(0.25), radius: 6, x: 0, y: 3)
         }
         .padding(.vertical, 16)
     }
    
    // MARK: - 奖励项目
    private func rewardItem(icon: String, title: String, description: String) -> some View {
        HStack(spacing: 16) {
            ZStack {
                // 图标背景光效
                Circle()
                    .fill(
                        RadialGradient(
                            colors: [
                                Color(red: 1.0, green: 0.7, blue: 0.3).opacity(0.3),
                                Color(red: 1.0, green: 0.6, blue: 0.2).opacity(0.1),
                                Color.clear
                            ],
                            center: .center,
                            startRadius: 5,
                            endRadius: 25
                        )
                    )
                    .frame(width: 50, height: 50)
                
                // 图标主体
                Image(systemName: icon)
                    .font(.system(size: 26, weight: .semibold))
                    .foregroundStyle(
                        LinearGradient(
                            colors: [
                                Color(red: 1.0, green: 0.8, blue: 0.4),
                                Color(red: 1.0, green: 0.6, blue: 0.2)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                    .shadow(color: Color(red: 1.0, green: 0.6, blue: 0.2).opacity(0.5), radius: 4, x: 0, y: 2)
            }
            
            VStack(alignment: .leading, spacing: 6) {
                Text(title)
                    .font(.system(size: 17, weight: .bold, design: .rounded))
                    .foregroundStyle(
                        LinearGradient(
                            colors: [
                                Color.white,
                                Color.white.opacity(0.9)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                    .shadow(color: .black.opacity(0.3), radius: 1, x: 0, y: 1)
                
                Text(description)
                    .font(.system(size: 15, weight: .medium))
                    .foregroundColor(.white.opacity(0.85))
                    .multilineTextAlignment(.leading)
                    .lineLimit(nil)
            }
            
            Spacer()
        }
        .padding(.horizontal, 20)
        .padding(.vertical, 18)
        .background(
            ZStack {
                // 主背景
                RoundedRectangle(cornerRadius: 16)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.08),
                                Color.white.opacity(0.04),
                                Color.white.opacity(0.02)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                
                // 边框光效
                RoundedRectangle(cornerRadius: 16)
                    .stroke(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.2),
                                Color.white.opacity(0.05),
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 1
                    )
            }
        )
        .shadow(color: .black.opacity(0.1), radius: 8, x: 0, y: 4)
    }
    
    // MARK: - 高级背景视图
    private var premiumBackgroundView: some View {
        ZStack {
            // 主背景渐变 - 橙色系
            LinearGradient(
                colors: [
                    Color(red: 1.0, green: 0.8, blue: 0.4),
                    Color(red: 1.0, green: 0.6, blue: 0.2),
                    Color(red: 0.9, green: 0.5, blue: 0.1),
                    Color(red: 0.8, green: 0.4, blue: 0.0)
                ],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            
            // 动态光效层 - 暖色调
            RadialGradient(
                colors: [
                    Color(red: 1.0, green: 0.9, blue: 0.6).opacity(0.25),
                    Color(red: 1.0, green: 0.8, blue: 0.4).opacity(0.15),
                    Color.clear
                ],
                center: UnitPoint(x: 0.3 + animationOffset * 0.4, y: 0.2 + animationOffset * 0.3),
                startRadius: 50,
                endRadius: 300
            )
            
            // 第二层光效 - 深橙色
            RadialGradient(
                colors: [
                    Color(red: 1.0, green: 0.7, blue: 0.3).opacity(0.18),
                    Color(red: 0.9, green: 0.5, blue: 0.1).opacity(0.10),
                    Color.clear
                ],
                center: UnitPoint(x: 0.7 - animationOffset * 0.3, y: 0.8 - animationOffset * 0.2),
                startRadius: 30,
                endRadius: 200
            )
            
            // 纹理层
            Rectangle()
                .fill(
                    LinearGradient(
                        colors: [
                            Color.white.opacity(0.02),
                            Color.clear,
                            Color.white.opacity(0.01)
                        ],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )
        }
        .ignoresSafeArea()
    }
    
    // MARK: - 动画控制
    private func startAnimations() {
        withAnimation(.easeInOut(duration: 3.0).repeatForever(autoreverses: true)) {
            animationOffset = 1.0
        }
        
        withAnimation(.easeInOut(duration: 2.5).repeatForever(autoreverses: true)) {
            glowAnimation = true
        }
    }
    
    // MARK: - 高级奖励项目
    private func premiumRewardItem(icon: String, iconColor: Color, title: String, description: String, accentColor: Color) -> some View {
        HStack(spacing: 16) {
            ZStack {
                // 图标背景光效
                Circle()
                    .fill(
                        RadialGradient(
                            colors: [
                                iconColor.opacity(0.3),
                                iconColor.opacity(0.1),
                                Color.clear
                            ],
                            center: .center,
                            startRadius: 5,
                            endRadius: 25
                        )
                    )
                    .frame(width: 50, height: 50)
                
                // 图标主体
                Image(systemName: icon)
                    .font(.system(size: 26, weight: .semibold))
                    .foregroundStyle(
                        LinearGradient(
                            colors: [
                                iconColor.opacity(0.9),
                                iconColor
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                    .shadow(color: iconColor.opacity(0.5), radius: 4, x: 0, y: 2)
            }
            
            VStack(alignment: .leading, spacing: 6) {
                Text(title)
                    .font(.system(size: 17, weight: .bold, design: .rounded))
                    .foregroundStyle(
                        LinearGradient(
                            colors: [
                                Color.white,
                                Color.white.opacity(0.9)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                    .shadow(color: .black.opacity(0.3), radius: 1, x: 0, y: 1)
                
                Text(description)
                    .font(.system(size: 15, weight: .medium))
                    .foregroundColor(.white.opacity(0.85))
                    .multilineTextAlignment(.leading)
                    .lineLimit(nil)
            }
            
            Spacer()
        }
        .padding(.horizontal, 20)
        .padding(.vertical, 18)
        .background(
            ZStack {
                // 主背景
                RoundedRectangle(cornerRadius: 16)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.08),
                                Color.white.opacity(0.04),
                                Color.white.opacity(0.02)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                
                // 边框光效
                RoundedRectangle(cornerRadius: 16)
                    .stroke(
                        LinearGradient(
                            colors: [
                                accentColor.opacity(0.3),
                                accentColor.opacity(0.1),
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 1
                    )
            }
        )
        .shadow(color: accentColor.opacity(0.2), radius: 8, x: 0, y: 4)
    }
    
    // MARK: - 辅助方法
    private func generateInviteCode() -> String {
        // 生成4位数字邀请码，72小时内不重复
        let currentTime = Date().timeIntervalSince1970
        let timeSlot = Int(currentTime / (72 * 3600)) // 72小时时间槽
        
        if let user = dataManager.currentUser {
            let userIdHash = user.id.hashValue
            let combinedHash = abs(userIdHash ^ timeSlot)
            let fourDigitCode = String(format: "%04d", combinedHash % 10000)
            return fourDigitCode
        }
        
        // 备用方案：基于时间生成4位数字
        let fallbackCode = String(format: "%04d", timeSlot % 10000)
        return fallbackCode
    }
    
    private func generateAndShowInviteCode() {
        // 检查用户是否已登录
        if dataManager.currentUser == nil {
            showingLoginAlert = true
            return
        }
        
        generatedCode = generateInviteCode()
        showGeneratedCode = true
    }
    
    private func copyInviteCode() {
        UIPasteboard.general.string = generatedCode
        alertMessage = language == "zh" ? "邀请码已复制到剪贴板" : "Invite code copied to clipboard"
        showingAlert = true
    }
}

// MARK: - 高级按钮样式
struct PremiumButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.96 : 1.0)
            .opacity(configuration.isPressed ? 0.9 : 1.0)
            .animation(.easeInOut(duration: 0.1), value: configuration.isPressed)
    }
}

// MARK: - ActivityViewController for sharing
struct ActivityViewController: UIViewControllerRepresentable {
    let activityItems: [Any]
    
    func makeUIViewController(context: Context) -> UIActivityViewController {
        let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
        // No updates needed
    }
}

#Preview {
    InviteView()
        .environmentObject(DataManager.shared)
}